import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Color;

public class vTablero2 extends JFrame implements ActionListener {
    private JPanel panelPpal = new JPanel ();
    private JPanel tablero = new JPanel ();
    private JPanel menu = new JPanel ();
    private JLabel info = new JLabel (" Pulsa un boton para marcar el inicio.");
    private JButton fTablero[][] = new JButton [8][8];
    private JButton bCalcular = new JButton("Saltar");
    private JButton bReiniciar = new JButton("Reiniciar");
    private Caballo c = new Caballo ();
    private boolean init = false;

    public vTablero2 () {
        int i = 0;
        int j = 0;
        
        setTitle("Salto del caballo");
        setSize(540,600);
        
        panelPpal.setLayout (new BorderLayout ());
        tablero.setLayout (new GridLayout (8,8));
        
        add (menu, BorderLayout.NORTH);
        add (tablero, BorderLayout.CENTER);
        add (info, BorderLayout.SOUTH);
        
        menu.add (bCalcular);
        menu.add (bReiniciar);
        
        bCalcular.addActionListener (this);
        bReiniciar.addActionListener (this);
        
        tablero.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4));
        
        for (i = 0; i < 8; i++) {
            for (j = 0; j < 8; j++) {
                fTablero[i][j] = new JButton ("-");
                tablero.add (fTablero[i][j]);
                if ((i % 2 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 2 == 0)) {
                    fTablero[i][j].setBackground(Color.black);
                }
                else {
                    fTablero[i][j].setBackground(Color.white);
                }
                fTablero[i][j].addActionListener (this);
            }
        }
    }
    
    public void actionPerformed(ActionEvent evento) {
        Object source = evento.getSource();
        int valor[] = new int[3];
        int i = 0;
        int j = 0;
        
        if (source == bReiniciar) {
            for (i = 0; i < 8; i++) {
                for (j = 0; j < 8; j++) {
                    fTablero[i][j].setText("-");
                    c = new Caballo ();
                    info.setText(" Pulsa un boton para marcar el inicio.");
                    init = false;
                }
            }
        } else if (source == bCalcular) {
            if (init == false) {
                info.setText(" Primero tiene que pulsar una casilla");
            } else {
                valor = c.nextNum();
                if (valor[2] != 0) {
                    fTablero[valor[0]][valor[1]].setText(valor[2] + "");
                } else {
                    info.setText(" No hay mas saltos que hacer.");
                }
            }
        } else {
            if (init == false) {
                for (i = 0; i < 8; i++) {
                    for (j = 0; j < 8; j++) {
                        if (source == fTablero[i][j]) {
                            c.procesar(i, j);
                            fTablero[i][j].setText(1 + "");
                            info.setText(" Pulse el boton \"Saltar\" para avanzar");
                            init = true;
                        }
                    }
                }
            }
        }
    }
}